/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generator;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author abosancic
 */
public class SQLiteParser {
    
    private String sqliteDLL;
    private Map<String, String> columns = new HashMap<>();
    private String tableName;
    private String primaryKeyName;

    public SQLiteParser(String sqliteDLL) {
        this.sqliteDLL = sqliteDLL;
        parse(sqliteDLL);
    }

    private void parse(String sqliteDLL) {
       
        String [] dll = sqliteDLL.split("\n");
        
            for (int j = 0; j < dll.length; j++) {
                
                dll[j] = dll[j].replaceAll(",", "").replaceAll("[\n\r]", "").trim(); //s1.replaceAll("[\n\r]", "");
                
                if(dll[j].startsWith("CREATE TABLE"))
                {
                    this.setTableName(dll[j].replace("CREATE TABLE", "").trim().replaceAll("\"", "").replace("(", "").trim());
                }
                else if(dll[j].startsWith("PRIMARY KEY") || dll[j].startsWith("primary key"))
                {
                    dll[j] = dll[j].replace("primary key", "PRIMARY KEY");
                    String pk = dll[j].replace("PRIMARY KEY", "").trim().replace("(", "").replace(")", "").replaceAll("\"", "").trim();
                    this.setPrimaryKeyName(pk.split(" ")[0] );
                }
                else if(dll[j].startsWith("\"") || (dll[j].length() > 3 && (!dll[j].toUpperCase().startsWith("CONSTRAINT"))))
                {
                    String tmp [] = dll[j].replaceAll("  ", " ").replaceAll("\"", "").split(" ");
                    int ind = tmp[1].indexOf("(");
                    if(ind > 0){
                        getColumns().put(tmp[0], tmp[1].substring(0, ind) );
                    }else{
                        getColumns().put(tmp[0], tmp[1] );
                    }
                }
                System.out.println(dll[j]);
            }
            
    }

    @Override
    public String toString() {
        return "SQLiteParser{" + "sqliteDLL=" + getSqliteDLL() + ",\n\n columns=" + getColumns() + ",\n\n tableName=" + getTableName() + ", primaryKeyName=" + getPrimaryKeyName() + '}';
    }

    /**
     * @return the sqliteDLL
     */
    public String getSqliteDLL() {
        return sqliteDLL;
    }

    /**
     * @param sqliteDLL the sqliteDLL to set
     */
    public void setSqliteDLL(String sqliteDLL) {
        this.sqliteDLL = sqliteDLL;
    }

    /**
     * @return the columns
     */
    public Map<String, String> getColumns() {
        return columns;
    }

    /**
     * @param columns the columns to set
     */
    public void setColumns(Map<String, String> columns) {
        this.columns = columns;
    }

    /**
     * @return the tableName
     */
    public String getTableName() {
        return tableName;
    }

    /**
     * @param tableName the tableName to set
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /**
     * @return the primaryKeyName
     */
    public String getPrimaryKeyName() {
        return primaryKeyName;
    }

    /**
     * @param primaryKeyName the primaryKeyName to set
     */
    public void setPrimaryKeyName(String primaryKeyName) {
        this.primaryKeyName = primaryKeyName;
    }

}
